/**
 * Created by leiweibo on 6/28/15.
 */

var app = angular.module('hospitalcontroller', []);
app.filter('offset', function () {
  return function (input, start) {
    start = parseInt(start, 10);
    return input.slice(start);
  };
});
app.controller('HospitalCtrl', function ($scope, ProvinceService, HospitalService, HospitalData) {

  $scope.provinces = ProvinceService.list(function(data) {
    $scope.provinces = data.provinces
  });


  $scope.callServer = function (tableState) {
    console.log("executed.")
    $scope.isLoading = true;

    var pagination = tableState.pagination;
    var pageSize = pagination.number || 20;
    var pageNo = Math.ceil((pagination.start + 1) / pageSize) || 1;

    HospitalService.list(pageNo, pageSize, tableState, function(res) {
      $scope.displayed = res.hospitals;
      tableState.pagination.numberOfPages = res.pageCount;
      $scope.isLoading = false;
    });
  };

  $scope.cancel = function() {
    window.location = '#/hospital_list';
  }

  $scope.edit = function(pos) {
    HospitalData.set($scope.displayed[pos]);
    window.location = "#/update_hospital";
  }

  if (HospitalData.get() && !$.isEmptyObject(HospitalData.get())) {
    var data = HospitalData.get();
    $scope.id = data._id;
    $scope.name = data.name;
    $scope.province = data.province._id;
    $scope.introduce = data.introduce;
    $scope.type = Boolean(data.type);
  }

  $scope.delete = function (pos) {
    bootbox.setDefaults("locale", "zh_CN");
    bootbox.confirm("确定删除该医院信息吗", function (result) {
      if (result == true) {
        var hospital = $scope.displayed[pos];
        if (hospital) {
          var data = {id: hospital._id};
          HospitalService.delete(data, function (res) {
            if (res.result == true) {
              $scope.displayed.splice(pos, 1);
            } else {
              alert('删除数据失败');
            }
          }, function (data, status) {
          });
        } else {
          alert("请刷新重试");
        }
      }
    });
  };

  $scope.createOrUpdate = function() {
    var formData = {
      id: $scope.id,
      name: $scope.name,
      province: $scope.province,
      type: +($scope.type),
      introduce: $scope.introduce
    }

    if ($scope.id) {
      HospitalService.update(formData, function (res) {
        if (res.result == true) {
          window.location = "#/hospital_list";
        } else {
          $scope.error_msg = res.message;
        }
      }, function (data, status) {
        if (status == 403) {
          $scope.error_msg = '无权限进行此操作';
        }

      })
    } else {
      HospitalService.create(formData, function(res) {
        if (res.result == true) {
          delete $scope.id,
            delete $scope.name,
            delete $scope.province,
            delete $scope.type,
            delete $scope.introduce
        } else {
          $scope.error_msg = res.message;
        }
      }, function(data, status) {
        $scope.error_msg = '无权限进行此操作';
      })
    }
  }

  $scope.add = function() {
    HospitalData.set(null);
    window.location = "#/create_hospital";
  }
})